package com.ewanghuiju.app.ui.home.adapter;

import android.graphics.Paint;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.ewanghuiju.app.R;
import com.ewanghuiju.app.component.ImageLoader;
import com.ewanghuiju.app.model.bean.response.PintuanShopListResponBean;
import com.ewanghuiju.app.util.StringUtil;
import com.makeramen.roundedimageview.RoundedImageView;

import java.util.List;

/**
 * @author sj  qq:751943509
 * @class 搜索结果列表
 * @time 2020/2/20
 */
public class SearchShopResultAdapter extends BaseMultiItemQuickAdapter<PintuanShopListResponBean, BaseViewHolder> {
//    public SearchShopResultAdapter(int layoutResId) {
//        super(layoutResId);
//    }

    private CountDownTimer countDownTimer;

    public SearchShopResultAdapter(List<PintuanShopListResponBean> data) {
        super(data);
        addItemType(PintuanShopListResponBean.RESULT_TYPE_SHOP, R.layout.item_pintuan_shop_hot);
        addItemType(PintuanShopListResponBean.RESULT_TYPE_TUAN, R.layout.item_history_record_pintuan);
    }

    @Override
    protected void convert(BaseViewHolder helper, PintuanShopListResponBean item) {

        TextView tv_go_cantuan = helper.getView(R.id.tv_go_cantuan);
        if(item.getUser_yes()==1){
            tv_go_cantuan.setText("已参团");
            tv_go_cantuan.setBackgroundResource(R.drawable.corner_999_50);
        }else{
            tv_go_cantuan.setText("去参团");
            tv_go_cantuan.setBackgroundResource(R.drawable.corner_primary_button_50);
        }

        switch (helper.getItemViewType()) {
            case PintuanShopListResponBean.RESULT_TYPE_SHOP:
                //商品
                TextView tv_title = helper.getView(R.id.tv_shop_title);
                StringUtil.getTextStrFormat(tv_title, item.getPeople_number() + "人团", item.getGoods_name());
                helper.setText(R.id.tv_spe_price, item.getSpe_price());
                helper.setText(R.id.iv_title_icon, item.getPeople_number() + "人团");
                helper.setText(R.id.tv_salse, "已拼" + item.getOff_group_sum() + "件");
                TextView tv_yuan = helper.getView(R.id.tv_yuan_price);
                tv_yuan.setText("￥" + item.getYuan_price());
                tv_yuan.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG); // 设置中划线并加清晰
                RoundedImageView riv = helper.getView(R.id.riv_item_icon);
                if (!TextUtils.isEmpty(item.getImage())) {
                    ImageLoader.load(mContext, item.getImage(), riv);
                }
                break;

            case PintuanShopListResponBean.RESULT_TYPE_TUAN:
                //拼团信息
                TextView tvSytime = helper.getView(R.id.tv_sytime);
                long currenTime = System.currentTimeMillis() / 1000;
                long endTime = item.getEnd_time();
                long seconds = endTime - currenTime;
                if (countDownTimer != null) {
                    countDownTimer.cancel();
                }
                countDownTimer = new CountDownTimer(seconds * 1000, 1000) {
                    public void onTick(long l) {
                        tvSytime.setText("剩余时间："+StringUtil.getTimeStr(l));
                    }

                    public void onFinish() {
                        //倒计时结束 刷新商品信息
//                        RxBus.getDefault().post(new SendEvent("", Constants.TYPE_GET_SHOP_INFO));
                    }
                }.start();

                TextView tv_pt_title = helper.getView(R.id.tv_shop_title);
                StringUtil.getTextStrFormat(tv_pt_title, item.getPeople_number() + "人团", item.getGoods_name());
                helper.setText(R.id.tv_title_icon, item.getPeople_number() + "人团");
//                helper.setText(R.id.tv_pintuan_no, "拼团号：" + item.getGroup_no());
                helper.setText(R.id.tv_price, item.getGroup_price());
                helper.setText(R.id.tv_spe_price, item.getResidue_user() + "人");
                RoundedImageView pt_riv = helper.getView(R.id.riv_item_icon);
                if (!TextUtils.isEmpty(item.getImage())) {
                    ImageLoader.load(mContext, item.getImage(), pt_riv);
                }
                RoundedImageView headImg = helper.getView(R.id.riv_item_head);
                ImageLoader.load(mContext, item.getHead_image(), headImg);
                helper.setText(R.id.tv_name, TextUtils.isEmpty(item.getNickname()) ? "" : ("发团人：" + item.getNickname()));
                helper.setText(R.id.tv_group_no, TextUtils.isEmpty(item.getGroup_no()) ? "" : ("拼团号：" + item.getGroup_no()));
                break;
        }


    }

    public void clearCountDownTimer() {
        if (countDownTimer != null) {
            countDownTimer.cancel();
            countDownTimer = null;
        }
    }
}
